package com.jiantong.work.impl.service.yangzhibaoxian;

/**
 * Created by ThinkPad on 2017/9/5.
 */

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceApplicationDao;
import com.jiantong.work.impl.dao.yangzhibaoxian.ProspectRecordDao;
import com.jiantong.work.model.yangzhibaoxian.ProspectRecord;
import com.jiantong.work.service.yangzhibaoxian.ProspectRecordService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 投保勘察
 */
@Slf4j
@Service
@RpcProvider
public class ProspectRecordServiceImpl implements ProspectRecordService {
    private ProspectRecordDao prospectRecordDao;

    @Autowired
    public ProspectRecordServiceImpl (ProspectRecordDao prospectRecordDao) {
        this.prospectRecordDao = prospectRecordDao;
    }

    @Override
    public Response<ProspectRecord> findById(Long id) {
        try {
            ProspectRecord prospectRecord = (ProspectRecord) prospectRecordDao.findById(id);
            if (prospectRecord == null) {
                log.error("prospectRecord(id={}) is not found", id);
                return Response.fail("prospectRecord.not.found");
            }

            return Response.ok(prospectRecord);
        } catch (Exception e) {
            log.error("failed to find prospectRecord id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("prospectRecord.find.fail");
        }
    }

    @Override
    public Response<List<ProspectRecord>> findByIds(List ids) {
        try {
            List<ProspectRecord> prospectRecordList =  prospectRecordDao.findByIds(ids);
            if (prospectRecordList == null) {
                log.error("prospectRecordList(ids={}) is not found", ids);
                return Response.fail("prospectRecordList not.found");
            }

            return Response.ok(prospectRecordList);
        } catch (Exception e) {
            log.error("failed to find prospectRecordList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("prospectRecordList.find.fail");
        }

    }

    @Override
    public Response<List<ProspectRecord>> list(Map map) {
        try {
            List<ProspectRecord> prospectRecordList = prospectRecordDao.list(map);
            if (prospectRecordList == null) {
                log.error("prospectRecordList(ids={}) is not found", map);
                return Response.fail("prospectRecordList.not.found");
            }

            return Response.ok(prospectRecordList);
        } catch (Exception e) {
            log.error("failed to find prospectRecordList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("prospectRecordList.find.fail");
        }
    }

    @Override
    public Response<Paging<ProspectRecord>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<ProspectRecord> prospectRecordPaging = prospectRecordDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);
            if (prospectRecordPaging == null) {
                log.error("prospectRecordPaging(ids={}) is not found", map);
                return Response.fail("prospectRecordPaging not.found");
            }

            return Response.ok(prospectRecordPaging);
        } catch (Exception e) {
            log.error("failed to find prospectRecordPaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("prospectRecordPaging.find.fail");
        }
    }

    @Override
    public Boolean update(ProspectRecord prospectRecord) {
        try {
            Boolean b =  prospectRecordDao.update(prospectRecord);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(ProspectRecord prospectRecord) {
        try {
            prospectRecordDao.create(prospectRecord);
            return  prospectRecord.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<ProspectRecord > prospectRecordList) {
        try {

            return prospectRecordDao.creates(prospectRecordList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }

}
